jetcrab\bytecode\statements/
if_statement.rs1use crate::ast::Node;
2use crate::vm::instructions::Instruction;
3use crate::vm::types::CodeAddress;
4
5use super::ControlFlowCore;
6
7pub fn generate_if_statement<T>(this: &mut T, node: &Node)
8where
9 T: ControlFlowCore,
10{
11 if let Node::IfStatement(stmt) = node {
12 this.visit_node(&stmt.test);
14
15 let jump_to_else_pos = this.instructions().len();
17 this.instructions()
18 .push(Instruction::JumpIfFalse(CodeAddress::new(0))); this.visit_node(&stmt.consequent);
22
23 let jump_over_else_pos = this.instructions().len();
25 this.instructions()
26 .push(Instruction::Jump(CodeAddress::new(0))); let else_start_pos = this.instructions().len();
30 this.instructions()[jump_to_else_pos] =
31 Instruction::JumpIfFalse(CodeAddress::new(else_start_pos));
32
33 if let Some(alt) = &stmt.alternate {
35 this.visit_node(alt);
36 }
37
38 let end_pos = this.instructions().len();
40 this.instructions()[jump_over_else_pos] = Instruction::Jump(CodeAddress::new(end_pos));
41 }
42}